/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import entity.Faq;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import model.FaqFacade;
import sessionBean.FAQSessionBeanLocal;

/**
 *
 * @author VietTien
 */
@ManagedBean
@SessionScoped
public class FAQBean {

    @EJB
    private FaqFacade faqFacade;
    @EJB
    private FAQSessionBeanLocal fAQSessionBean;

    /** Creates a new instance of FAQBean */
    public FAQBean() {
        faq = new Faq();
        currentDate = new Date();
    }
    private Faq faq;
    Date currentDate;

    public Date getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(Date currentDate) {
        this.currentDate = currentDate;
    }

    public List getAllFAQ() {
        return fAQSessionBean.getAllFAQs();
    }

    public FAQSessionBeanLocal getfAQSessionBean() {
        return fAQSessionBean;
    }

    public void setfAQSessionBean(FAQSessionBeanLocal fAQSessionBean) {
        this.fAQSessionBean = fAQSessionBean;
    }

    public Faq getFaq() {
        return faq;
    }

    public void setFaq(Faq faq) {
        this.faq = faq;
    }

    public String detailFAQs(Faq f) {
        //guest page
        this.faq = f;
        return "FAQDETAIL";
    }

    public String listFAQs() {
        //guest page
        return "FAQLIST";
    }

    public String navigator(String page) {
        return page;
    }

    public void deleteFAQ(ActionEvent evt) {
        fAQSessionBean.deleteFAQ(faq);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "Delete successfull !!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public boolean checkAllValidator() {
        if (faq.getQuestion().trim().length() <= 0) {

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message", "Question not be blank!!");
            FacesContext.getCurrentInstance().addMessage(null, message);
            return false;
        }

        if (faq.getAnswer().trim().length() <= 0) {

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message", "Answer  not be blank!!");
            FacesContext.getCurrentInstance().addMessage(null, message);
            return false;
        }
        String qFaq = faq.getQuestion().trim();
        qFaq = qFaq.replaceAll("\\s+", " ");
        if (faqFacade.findByName(qFaq).size() > 0) {
            JsfUtil.addErrorMessage("Question already exist !");
            return false;
        }

        return true;
    }

    public void insertFAQ() {
        if (checkAllValidator()) {
            faq.setCreatedDate(currentDate);
            fAQSessionBean.insertFAQ(faq);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "Insert  new FAQ successfull !!");
            FacesContext.getCurrentInstance().addMessage(null, message);
            faq.setAnswer("");
            faq.setQuestion("");

        }

    }

    public boolean checkExistQuestion(String question) {
        boolean flag = false;
        List<Faq> lst = getAllFAQ();
        for (Faq f : lst) {
            if (f.getQuestion().equals(question)) {
                flag = true;
            }
        }
        return flag;
    }

    public String updateFAQ() {
        if (checkAllValidator()) {
            fAQSessionBean.updateFAQ(faq);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "Update successfull !!");
            FacesContext.getCurrentInstance().addMessage(null, message);
            faq.setAnswer("");
            faq.setQuestion("");
        }


        return "ManageFAQs";
    }

    public String prepareInsert() {
        this.faq.setAnswer(null);
        this.faq.setQuestion(null);
        return "insertFAQ";
    }
}
